package pers.qtong.tank;

import java.awt.*;

public class Bullet extends GameObject{
    private static final int SPEED = PropertiesMgr.getInstance().getInt("bulletSpeed");

    private Group group;

    public static int width = ResourceMgr.bulletD.getWidth();
    public static int height = ResourceMgr.bulletD.getHeight();

    private boolean living = true;
    
    private int x;
    private int y;
    private Dir dir;

    public Rectangle rectangle = new Rectangle();

    public Bullet(int x, int y, Dir dir, Group group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;

        rectangle.width = width;
        rectangle.height = height;


        /*rectangle.x = x;
        rectangle.y = y;*/
        GameModel.getInstance().add(this);
    }

    public Group getGroup() {
        return group;
    }

    @Override
    public void paint(Graphics g) {
        if(!living) {
            GameModel.getInstance().remove(this);
        }
        switch (dir) {
            case LEFT:
                g.drawImage(ResourceMgr.bulletL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(ResourceMgr.bulletR, x, y, null);
                break;
            case UP:
                g.drawImage(ResourceMgr.bulletU, x, y, null);
                break;
            case DOWN:
                g.drawImage(ResourceMgr.bulletD, x, y, null);
                break;
        }
        move();
    }

    private void move() {
        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
        }
        rectangle.x = this.x;
        rectangle.y = this.y;
        //当子弹出屏幕时
        if(x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) living = false;
    }

    //子弹打坦克 （碰撞检测）
    /*public boolean collideWidth(Tank tank) {
        if(this.group == tank.getGroup()) {
            return true;
        }
        if(rectangle.intersects(tank.rectangle)) {
            die();
            tank.die();
            int ex = tank.getX() + Tank.WIDTH / 2 - Bullet.width / 2;
            int ey = tank.getY() + Tank.HEIGHT / 2 - Bullet.height / 2;
            GameModel.getInstance().add(new Explode(ex, ey));
            return false;
        }
        return true;
    }*/

    public void die() {
        living = false;
    }
}
